﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OnTheWay.Command;

namespace OnTheWay.Functions
{
    /// <summary>
    /// FunctionPanelHeader.xaml 的交互逻辑
    /// </summary>
    public partial class FunctionPanelHeader : ItemsControl
    {
        // Fields
        public static readonly DependencyProperty FunctionPanelProperty = DependencyProperty.Register("FunctionPanel", typeof(FunctionPanel), typeof(FunctionPanelHeader), new UIPropertyMetadata(null));

        // Methods
        public FunctionPanelHeader()
        {
            this.CommandClose = new DelegateCommand(OnCommandClose, OnCanCommandClose, "Close", "关闭");
            this.CommandSwitch = new DelegateCommand(OnCommandSwitch, OnCanCommandSwitch, "Switch", "切换");
            this.InitializeComponent();
        }

        private void FunctionPanelHeaderItem_OnClose(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if ((element != null) && (element.DataContext is FunctionEntity))
            {
                this.OnCommandClose(element.DataContext);
            }
        }

        private void FunctionPanelHeaderItem_OnSwitch(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if ((element != null) && (element.DataContext is FunctionEntity))
            {
                this.OnCommandSwitch(element.DataContext);
            }
        }

        public bool OnCanCommandClose(object obj)
        {
            return true;
        }

        public bool OnCanCommandSwitch(object obj)
        {
            return true;
        }

        public void OnCommandClose(object obj)
        {
            FunctionEntity entity = obj as FunctionEntity;
            if ((entity != null) && (entity.Ctrl.IFunPanel != null))
            {
                entity.Ctrl.IFunPanel.CloseFunction(entity.Ctrl);
            }
        }

        public void OnCommandSwitch(object obj)
        {
            FunctionEntity entity = obj as FunctionEntity;
            if ((entity != null) && (entity.Ctrl.IFunPanel != null))
            {
                FunctionPanel.SwitchFunction(entity.Ctrl);
            }
        }

        // Properties
        public DelegateCommand CommandClose { get; set; }

        public DelegateCommand CommandSwitch { get; set; }

        public FunctionPanel FunctionPanel
        {
            get
            {
                return (FunctionPanel)base.GetValue(FunctionPanelProperty);
            }
            set
            {
                base.SetValue(FunctionPanelProperty, value);
            }
        }
    }

}
